package com.assignment.health;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.assignment.health.databinding.ActivityLoginBinding;
import com.google.gson.JsonObject;
import com.net.http.NetClient;
import com.user.UserInformation;


public class LoginActivity extends AppCompatActivity {

    class MessageType {
        public static final int loginReturn = 0x01;
        public static final int debugInfor = 0x02;
    }

    private AppBarConfiguration appBarConfiguration;
    private ActivityLoginBinding binding;

    private Button loginButton;
    private EditText textUsername;
    private EditText textPassword;
    private TextView textLogStatus;

    private Handler loginHandler;

    private String username;
    private String password;

    private void bindViews() {
        loginButton = (Button)findViewById(R.id.button_login);
        textUsername = (EditText)findViewById(R.id.editTextUsername);
        textPassword = (EditText)findViewById(R.id.editTextPassword);
        textLogStatus = (TextView)findViewById(R.id.textView_login);

        textUsername.setText("");
        textPassword.setText("");
        textLogStatus.setText("");
    }

    @SuppressLint("HandlerLeak")
    private void setHandler() {
        loginHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == MessageType.loginReturn) {
                    JsonObject json = (JsonObject)msg.obj;
                    if(json == null) {
                        textLogStatus.setText(R.string.login_wrong_textview);
                    }
                    else {
                        String status = json.get("status").getAsString();
                        if (status != null && status.contentEquals("OK")) {
                            textLogStatus.setText("");

                            UserInformation.available = 1;
                            UserInformation.token = json.get("token").getAsString();

                            Intent MainIntent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(MainIntent);

                        } else {
                            textLogStatus.setText(R.string.login_failure_textview);
//                            textLogStatus.setText(status);
                        }
                    }
                    loginButton.setEnabled(true);
                    textUsername.setEnabled(true);
                    textPassword.setEnabled(true);
                }
                else if(msg.what == MessageType.debugInfor) {
                    textLogStatus.setText((String)msg.obj);
                }
            }
        };
    }

    private void setActions() {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginButton.setEnabled(false);
                textUsername.setEnabled(false);
                textPassword.setEnabled(false);
                username = textUsername.getText().toString();
                password = textPassword.getText().toString();

                textUsername.setText("");
                textPassword.setText("");

                new Thread(new Runnable(){
                    public void run() {
                        JsonObject json = NetClient.GetLogin(username, password);
                        loginHandler.sendMessage(loginHandler.obtainMessage(MessageType.loginReturn, json));
                    }
                }).start();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        bindViews();
        setHandler();
        setActions();
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_login);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}